package com.niit.agricstore.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageInfo;
import com.niit.agricstore.entity.Cart;
import com.niit.agricstore.entity.vo.CartQureyVo;
import com.niit.agricstore.entity.vo.ProductVo;
import com.niit.agricstore.service.CartService;
import com.niit.agricstore.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/cart")
public class CartController {
    @Autowired
     CartService cartService;


    //购物车中增加当前产品数据
    @PostMapping("/addProduct")
    public Result addProduct(@RequestBody Cart cart) {
            if (cartService.findAllByProductId(cart.getCartProductId(),cart.getCartUid())!=null) {
                Cart findCart = cartService.findAllByProductId(cart.getCartProductId(),cart.getCartUid());
                int newSale = findCart.getCartProductSale()+1;
                int newPrice = findCart.getCartProductPrice() * newSale;
                System.out.println("=====================newSale=========================="+newSale);
                cartService.updateCartProductSaleAndCartPriceByCartId(newSale, newPrice, findCart.getCartId());
                return Result.success("102", "购物车已存在该商品，已经为您自动+1~~", cart);
            }
            int rows=cartService.addProduct(cart);
            if(rows>0){
            return Result.success("200","加上了~",cart);}else {
            return Result.error("200","没加上");
        }
    }
    @GetMapping("/findAllByCartId/{cartId}")
    public Result<List<Cart>> findAllByCartId(@PathVariable Integer cartId ) {
        List<Cart> cartList = cartService.findAllByCartId(cartId);
        return Result.success(cartList);
    }
    @GetMapping("/findAllByUid/{uid}")
    public Result<List<Cart>> findAllByCartUid(@PathVariable Long uid ) {
        List<Cart> cartList = cartService.findAllByUid(uid);
        return Result.success(cartList);
    }

    @GetMapping("/findAllByCartChoose/{cartChoose}")
    public Result<List<Cart>> findAllByCartChoose( @PathVariable String cartChoose) {
        List<Cart> cartList=cartService.findAllByCartChoose(cartChoose);
        return Result.success(cartList);
    }

    @DeleteMapping("/deleteByCartUid/{cartUid}")
    public Result  deleteByCartUid(@PathVariable long cartUid) {
        int rows=cartService.deleteByCartUid(cartUid);
        if (rows > 0){
            return Result.success("200","删没了~",rows);
        }else {
            return Result.error("200","没删掉");
        }
    }


    @DeleteMapping("/deleteByCartProductId/{cartProductId}")
    public Result deleteByCartProductId(@PathVariable int cartProductId) {
        int rows=cartService.deleteByCartProductId(cartProductId);
        if (rows > 0){
            return Result.success("200","删没了~",rows);
        }else {
            return Result.error("200","没删掉");
        }
    }

    @PostMapping("/insertAll")
    public Result insertAll( Cart cart) {
        int rows=cartService.insertAll(cart);
        if (rows > 0){
            return Result.success("200","加上了~",cart);
        }else {
            return Result.error("200","没加上");
        }
    }


    @PostMapping("/insertSelective")
    public Result insertSelective( Cart cart) {
        int rows=cartService.insertSelective(cart);
        if (rows > 0){
            return Result.success("200","加上了~",cart);
        }else {
            return Result.error("200","没加上");
        }
    }

    @PutMapping("/updateCartProductSaleAndCartPriceByCartProductId")
    public Result updateCartProductSaleAndCartPriceByCartProductId(@RequestParam int cartProductSale, @RequestParam int cartPrice, @RequestParam int cartProductId) {
        int rows=cartService.updateCartProductSaleAndCartPriceByCartProductId(cartProductSale, cartPrice,cartProductId);
        if (rows > 0){
            return Result.success("200","改好了~",cartProductSale);
        }else {
            return Result.error("200","没改好");
        }
    }

    @PutMapping("/updateCartChoose")
    public Result updateCartChoose(@RequestParam String cartChoose,@RequestParam Long cartUid) {
        int rows=cartService.updateCartChoose(cartChoose,cartUid);
        if (rows > 0){
            return Result.success("200","改好了~",cartChoose);
        }else {
            return Result.error("200","没改好");
        }
    }

    @PutMapping("/updateCartState")
    public Result updateCartState(@RequestParam String cartState,@RequestParam int cartProductId ) {
        int rows=cartService.updateCartState(cartState,cartProductId);
        if (rows > 0){
            return Result.success("200","改好了~",cartState);
        }else {
            return Result.error("200","没改好");
        }

    }
    @GetMapping("/cartByPage/{pageNum}/{pageSize}")
    public Result getCartByPage(@PathVariable("pageNum") String pageNum,
                                @PathVariable("pageSize") String pageSize) {
        int pageNumi = Integer.valueOf(pageNum);
        int pageSizei = Integer.valueOf(pageSize);
        PageInfo<Cart> cartList = cartService.findCartAllByPage(pageNumi, pageSizei);
        return Result.success(cartList);
    }
    @GetMapping("/page")
    public Result getCartsByPage(@RequestParam int page, @RequestParam int pageSize) {
        Page<Cart> cartPage = cartService.getCartsByPage(page, pageSize);
        return Result.success(cartPage);
    }
    @DeleteMapping("/delByCartId")
    public Result  delByCartId(@RequestParam long cartId) {
        int rows=cartService.delByCartId(cartId);
        if (rows > 0){
            return Result.success("200","删没了~",rows);
        }else {
            return Result.error("200","没删掉");
        }
    }
}
